//
//  NEDConsoleURLSchemeViewController.m
//  NEDBugConsole_Example
//
//  Created by 98du on 2021/12/15.
//  Copyright © 2021 林宁宁. All rights reserved.
//

#import "NEDConsoleURLSchemeViewController.h"
#import "NEDDBConsoleSetting.h"
#import "NEDConsole.h"

@interface NEDConsoleURLSchemeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (retain, nonatomic) UITextField * textView;

@property (retain, nonatomic) UITableView * tableView;

@end

@implementation NEDConsoleURLSchemeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"链接路由跳转";
    
    self.textView = [UITextField new];
    self.textView.placeholder = @"请输入链接";
    self.textView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    self.textView.font = [UIFont systemFontOfSize:18];
    self.textView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 60);
    self.textView.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.textView];
    
    UIButton * goBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [goBtn setTitle:@"打开" forState:(UIControlStateNormal)];
    goBtn.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5];
    [goBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [goBtn addTarget:self action:@selector(goAction) forControlEvents:(UIControlEventTouchUpInside)];
    goBtn.frame = CGRectMake(0, 60, CGRectGetWidth(self.view.frame), 60);
    [self.view addSubview:goBtn];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 130, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 130) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 44;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableHeaderView = [UIView new];
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    
    if(![NEDDBConsoleSetting sharedInstance].schemeURLs){
        [NEDDBConsoleSetting sharedInstance].schemeURLs = @[];
    }
}

- (void)goAction{
    if(self.textView.text.length > 0){
        NSMutableArray * list = [NEDDBConsoleSetting sharedInstance].schemeURLs.mutableCopy;
        [list removeObject:self.textView.text];
        [list insertObject:self.textView.text atIndex:0];
        
        [NEDDBConsoleSetting sharedInstance].schemeURLs = list;
        [self.tableView reloadData];
        
        if([NEDConsole console].routerHandle){
            [NEDConsole console].routerHandle(self.textView.text);
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [NEDDBConsoleSetting sharedInstance].schemeURLs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.text = [NEDDBConsoleSetting sharedInstance].schemeURLs[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString * url = [NEDDBConsoleSetting sharedInstance].schemeURLs[indexPath.row];
    self.textView.text = url;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction * delete = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSMutableArray * list = [NEDDBConsoleSetting sharedInstance].schemeURLs.mutableCopy;
        [list removeObjectAtIndex:indexPath.row];
        [NEDDBConsoleSetting sharedInstance].schemeURLs = list;
        [tableView reloadData];
    }];
    return @[delete];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.textView resignFirstResponder];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
